<?php


namespace app\adminapi\dao\user;


use app\common\dao\BaseHotelDao;
use app\common\model\user\User;
use app\common\model\user\UserType;

/**
 * @projectName src
 */
class UserDao extends BaseHotelDao
{
    public static function add(array $params)
    {
        User::create([
            'user_name' => $params['user_name'],
            'user_gender' => $params['user_gender'],
            'user_idno' => $params['user_idno'],
            'user_type' => $params['user_type'],
            'user_phone' => $params['user_phone'],
        ]);
    }

    public static function edit(array $params): bool
    {
        try {
            User::update([
                'id' => $params['id'],
                'user_name' => $params['user_name'],
                'user_gender' => $params['user_gender'],
                'user_idno' => $params['user_idno'],
                'user_type' => $params['user_type'],
                'user_phone' => $params['user_phone'],
            ]);
            return true;
        } catch (\Exception $e) {
            self::setError($e->getMessage());
            return false;
        }
    }

    public static function delete(array $params)
    {
        User::destroy($params['id']);
    }

    public static function detail(array $params): array
    {
        return User::with('userType')->findOrEmpty($params['id'])->toArray();
    }

    public static function getAllData()
    {
        return User::with('userType')->order('id', 'desc')->select()->toArray();
    }
}